<?php

/**
 * Class for livechat dialogue/conversation items
 *
 * @access 	public
 * @author      Rob
 * @copyright	ResMania 2009-present all rights reserved.
 * @version	1.0
 * @link	http://docs.resmania.com/index.php/LiveChat_Module
 * @since  	09-2010
 */
class RM_LiveChatDialogue extends RM_Model_Flexible {

    protected $_name = 'rm_livechat_dialogue';

    /**
     * insert a new chat item
     *
     * @param array $data
     * @return object
     */
    public function insertDialogue($data) {
        return parent::insert($data);
    }

    /**
     * gets chat items that are not yet marked as sent.
     *
     * @param string    $chat_id    the chat id
     * @param bool      $operator   used to switch between updating operator or user (true=operator)
     * @return object
     */
    public function getDialogue($chat_id, $operator) {


        $sql = "
            SELECT
                *
            FROM
                " . $this->_name . "
            WHERE
                chat_id='" . $chat_id . "'
            ";

        if ($operator) {
            $sql .= " AND sent_operator = '0'";
        } else {
            $sql .= " AND sent_user = '0'";
        }

        return $this->_getBySQL($sql);
    }

    /**
     * used to acknowledge that a message has been sent to the console/user end]
     * so that it is not resent
     *
     * @param string    message_id  the unique id of the message
     * @param bool      $operator   used to switch between updating operator or user (true=operator)
     * @return object
     */
    public function acknowledgeMessage($message_id, $operator) {

        if ($operator) {
            $data["sent_operator"] = 1;
        } else {
            $data["sent_user"] = 1;
        }
        $where = "id='" . $message_id . "'";

        return parent::update($data, $where);
    }

}
?>